//
//  MYAppHelper.m
//  MYOnline
//
//  Created by Robin on 16/8/8.
//  Copyright © 2016年 HolyShit. All rights reserved.
//

#import "MYAppHelper.h"

@implementation MYAppHelper

+(MYAppHelper *)shareHelper
{
    static MYAppHelper *helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [MYAppHelper new];
    });
    return helper;
}

-(instancetype)init
{
    if(self = [super init]){
        [self listenNetWork];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

/**
 *  监听网络
 */
-(void)listenNetWork
{
    AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [RACObserve(manager, networkReachabilityStatus) subscribeNext:^(NSNumber *status) {
        self.networkStatus = [status intValue];
        switch (self.networkStatus) {
            case AFNetworkReachabilityStatusNotReachable:{
                self.hasNetWork = NO;
                NSError *error = [NSError errorWithDomain:@"com.pba.meili" code:-10086 userInfo:@{@"msg" : @"网络不见了，请检查网络"}];
                [UIWindow showError:error];
            }
                break;
            case AFNetworkReachabilityStatusUnknown:
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                self.hasNetWork = YES;
                break;
        }
    }];
}
/**
 *  监听键盘
 */
- (void)noticeShowKeyboard:(NSNotification *)notification {
    self.isKeyboardVisible = YES;
}

- (void)noticeHideKeyboard:(NSNotification *)notification {
    self.isKeyboardVisible = NO;
}

- (CGFloat)rateOfHeight
{
    if(IS_47_INCH){
        return 1;
    }
    if(IS_55_INCH){
        return 1.103;
    }
    if(IS_4_INCH){
        return 0.851;
    }
    return 0.719;
}

- (CGFloat)rateOfWidth
{
    if(IS_47_INCH){
        return 1;
    }
    if(IS_55_INCH){
        return 1.104;
    }
    return 0.853;
}

#pragma mark - about uuid

static NSString *const MYDeviceUUIDKey = @"MYDeviceUUIDKey";

- (NSString *)getUUID
{
    NSString *uuidStr = [[NSUserDefaults standardUserDefaults] objectForKey:MYDeviceUUIDKey];
    if (![uuidStr isNotBlank]) {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        uuidStr = (__bridge_transfer  NSString *)string;
        [[NSUserDefaults standardUserDefaults] setObject:uuidStr forKey:MYDeviceUUIDKey];
    }
    return uuidStr;
}

static NSString *const MYServiceName = @"HolySit";
static NSString *const MYServiceAccount = @"UUID";

- (NSString *)getDeviceId
{
    NSString * uuidStr = [YYKeychain getPasswordForService:MYServiceName account:MYServiceAccount];
    if (![uuidStr isNotBlank])
    {
        uuidStr = [self getUUID];
        [YYKeychain setPassword:uuidStr forService:MYServiceName account:MYServiceAccount];
    }
    return uuidStr;
}

@end
